package com.flying.classmember.method;

import java.util.Arrays;

/**
 * @ClassName: MethodTest1
 * @createDate: 2023/6/21 14:00
 * @author: liu
 * @description:知识点:可变参数
 */
public class MethodTest1 {
    public static void main(String[] args) {

        long a = sum(1, 2, 3, 4, 5, 6, 7);
        System.out.println(a);
        long b = sum();
        System.out.println(b);

        int max = max(9, 2, 3, 4, 5);
        System.out.println(max);

        int max1 = max(6);
        System.out.println(max1);

        String str = concat("12", "你好", "hello world");
        System.out.println(str);

        String str1 = concat();
        System.out.println(str1);

        boolean d = isEven(2, 1, 2, 3);
        System.out.println(d);

        boolean e = isEven(1);
        System.out.println(e);

        boolean f = isEven();
        System.out.println(f);
    }

    // 1. 声明方法，求 0-n个整数的累加和，如果没有传参，就返回0
    public static long sum(int...nums){
        long sum=0;
        if(nums.length==0) return 0;
        for (int num : nums) {
            sum+=num;
        }
        return sum;
    }

    //  2定义方法，求1-n的整数中最大值

    public static int max(int a,int...others){
        // 先创建一个比others长度+1 的数组
        int[] tempOthers=new int[others.length+1];

        // 将others的元素复制给新数组
        System.arraycopy(others, 0, tempOthers, 0, others.length);
        System.out.println(Arrays.toString(tempOthers));

        //  将参数 a，追加到新数组中
        tempOthers[tempOthers.length-1]=a;
        System.out.println(Arrays.toString(tempOthers));

        //  将数组进行排序《找出最大值
        Arrays.sort(tempOthers);
        if(tempOthers.length==1) return a;
        return tempOthers[tempOthers.length-1];
    }

    // 3.定义方法，求0-n个字符串的拼接结果
    public static String concat(String...strings){
      String str="";
        for (String string : strings) {
            str+=string;
        }
        if(str.equals("")) return null;
        return str;
    }

    //  判断0-n个整数是否都是偶数，如果都是偶数，返回true，否则返回false
    public static boolean isEven(int...nums){
        boolean isEven=false;
        for (int num : nums) {
            if(num%2==0){
                return !isEven;
            }
        }
        return isEven;

    }
}
